package com.mida.app.report.process.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.mida.app.report.process.DocReportServiceLockProcess;
import com.mida.app.report.service.DocReportServiceLockService;
import com.mida.app.report.vo.DocReportListVo;
import com.mida.bms.report.api.DocReportServiceLockApi;
import com.mida.bms.report.entity.DocReportServiceLock;
import com.mida.dms.mdm.dto.UserDto;
import com.mida.dms.mdm.vo.provider.EmployeeInfoVo;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: DocReportServiceLockProcessImpl
 * @Description: TODO
 * @Author: wang xiao le
 * @Date: 2021/8/9 14:40
 **/
@Service
@Slf4j
public class DocReportServiceLockProcessImpl  extends BaseProcess implements DocReportServiceLockProcess {


    public static final long LOCK = 45989833557934080L;
    public static final long UNLOCK = 45989877979807744L;

    @Autowired
    private DocReportServiceLockApi docReportServiceLockApi;

    @Autowired
    private DocReportServiceLockService docReportServiceLockService;

    @Override
    public ListResult<String> select(DocReportServiceLock docReportServiceLock) {
        ListResult<String> result = new ListResult<>();
        if (checkData(docReportServiceLock, result)) {
            //校验数据合法性
            return result;
        }
        UserDto userDto = gainUserPart();
        //查询锁表
        docReportServiceLock.setStatus(LOCK);
        DocReportServiceLock data = docReportServiceLockApi.select(docReportServiceLock).getData();
        //1.锁表存在
        if (ObjectUtils.isNotEmpty(data)) {
            //不是同一用户
            if (!userDto.getEmployeeSoid().equals(data.getLockUserSoid())) {
                ListResult<EmployeeInfoVo> oneBySoid = mdmService.findOneBySoid(data.getLockUserSoid(), false);
                result.setMessage("当前报告已锁定");
                result.setStatus(500);
                result.setData(oneBySoid.getData().getEmployeeName());
                return result;
            }
        }
        buildListSuccess(result, null);
        return result;
    }

    @Override
    public ListResult<String> add(DocReportServiceLock docReportServiceLock) {
        ListResult<String> result = new ListResult<>();
        if (checkData(docReportServiceLock, result)) {
            //校验数据合法性
            return result;
        }
        UserDto userDto = gainUserPart();

        //查询锁表
        docReportServiceLock.setStatus(LOCK);
        DocReportServiceLock data = docReportServiceLockApi.select(docReportServiceLock).getData();
        //1.锁表存在
        if (ObjectUtils.isNotEmpty(data)) {
            //不是同一用户
            if (!userDto.getEmployeeSoid().equals(data.getLockUserSoid())) {
                ListResult<EmployeeInfoVo> oneBySoid = mdmService
                        .findOneBySoid(data.getLockUserSoid(),false);
                result.setMessage("当前报告已锁定");
                result.setStatus(500);
                result.setData(oneBySoid.getData().getEmployeeName());
                return result;
            }
        } else {
            //2.锁表不存在(无锁)
            docReportServiceLockService.saveLock(docReportServiceLock, userDto);
        }
        buildListSuccess(result, null);
        return result;
    }

    @Override
    public ActionResult delete(DocReportServiceLock docReportServiceLock) {
        ActionResult actionResult = new ActionResult();
        if (checkDeleteData(docReportServiceLock, actionResult)) {
            //校验删除锁数据入参合法性
            return actionResult;
        }
        UserDto userDto = gainUserPart();
        return  docReportServiceLockService.delete(docReportServiceLock,userDto);
    }



    /**
     * @return boolean
     * @Author WXL
     * @Description 校验查询锁表数据入参
     * @Date 2021/8/8
     * @Param [docReportServiceLock, result] 锁对象
     **/
    private boolean checkData(DocReportServiceLock docReportServiceLock, ListResult<String> result) {
        if (ObjectUtils.isEmpty(docReportServiceLock.getRequestSoid())) {
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("申请单标识不能为空");
            return true;
        }
        if (ObjectUtils.isEmpty(docReportServiceLock.getExamCheckSoid())) {
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("检查号不能为空");
            return true;
        }
        if (ObjectUtils.isEmpty(docReportServiceLock.getPolicySoid())) {
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("策略标识不能为空");
            return true;
        }
        return false;
    }

    /**
     * @return boolean
     * @Author WXL
     * @Description 校验删除锁数据入参合法性
     * @Date 2021/8/8
     * @Param [docReportServiceLock, actionResult]
     **/
    private boolean checkDeleteData(DocReportServiceLock docReportServiceLock, ActionResult actionResult) {
        if (ObjectUtils.isEmpty(docReportServiceLock.getRequestSoid())) {
            actionResult.setStatus(HttpStatus.BAD_REQUEST.value());
            actionResult.setMessage("申请单标识不能为空");
            return true;
        }
        if (ObjectUtils.isEmpty(docReportServiceLock.getExamCheckSoid())) {
            actionResult.setStatus(HttpStatus.BAD_REQUEST.value());
            actionResult.setMessage("检查号不能为空");
            return true;
        }
        if (ObjectUtils.isEmpty(docReportServiceLock.getPolicySoid())) {
            actionResult.setStatus(HttpStatus.BAD_REQUEST.value());
            actionResult.setMessage("策略标识不能为空");
            return true;
        }
        return false;
    }
}
